package com.tedu.weibo.aspect;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/*
用于为所有Controller处理请求的方法记录日志

 */
@Component
@Aspect
@Slf4j
public class LogAspect {
    @Before("execution(public  com.tedu.weibo.result.JsonResult com.tedu.weibo.controller.*.*(..))")
    public void before(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        log.info("进入类：{}，方法：{}",className,methodName);
        System.out.println("开始调用"+className+"."+methodName+"()方法");
    }
    @AfterReturning(
            value = "execution(public com.tedu.weibo.result.JsonResult com.tedu.weibo.controller.*.*(..))",
            returning = "result"
    )
    public void afterReturning(JoinPoint joinPoint,Object result){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        log.info("退出类：{}，方法：{}",className,methodName);
        System.out.println("结束"+className+"."+methodName+"()方法"+"，返回值为："+result);
    }
    @AfterThrowing(
            value = "execution(public com.tedu.weibo.result.JsonResult com.tedu.weibo.controller.*.*(..))",
            throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Throwable e){

        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        log.info("异常类：{}，方法：{}",className,methodName);
        System.out.println("异常"+className+"."+methodName+"()方法"+e.getMessage());
    }
    @After("execution(public com.tedu.weibo.result.JsonResult com.tedu.weibo.controller.*.*(..))")
    public void after(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        log.info("退出类：{}，方法：{}",className,methodName);
        System.out.println("退出"+className+"."+methodName+"()方法");
    }

}
